<?php

namespace app\admin\controller\post;

use app\common\controller\Backend;

class Category extends Backend
{
    protected $model = null;
    //关联查询的意思
    protected $relationSearch = true;
    public function __construct()
    {
        parent::__construct();
        $this->PostModel = model("Post.Post");
        $this->CategoryModel = model("Post.Category");
        $this->CollectModel = model("Post.Collect");
        $this->CommentModel = model("Post.Comment");
        $this->BusinessModel = model("Business.Business");
    }
    public function index()
    {
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $total = $this->CategoryModel
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count(); //查询总数

            $list = $this->CategoryModel
                ->where($where)
                ->order($sort,  $order)
                ->limit($offset, $limit)
                ->select(); //查询数据
            // echo $this->OrderModel->getlastSql();
            // exit;
            // halt(collection($list)->toArray());
            $result = array("total" => $total, "rows" => $list);
            return json($result);
        }
        return $this->view->fetch();
    }
 
    public function add()
    {
        if ($this->request->isAjax()) 
        {
            $params=$this->request->param('row/a');
            $result=$this->CategoryModel->validate('common/Post/Category')->save($params);
            if($result === FALSE)
            {
                $this->error($this->CategoryModel->getError());
                exit;
            }
            $this->success();
            exit;
        }
        return $this->view->fetch();
    }
    public function edit($ids = null)
    {
        $row=$this->CategoryModel->find($ids);
        if(!$row)
        {
            $this->error(__('No Results were found'));
            exit;
        }
        // halt($row->toArray());
        $this->assign("row", $row);

        if ($this->request->isAjax()) 
        {
            $params=$this->request->param('row/a');
            // halt($params);
            $params['id']=$ids;
            $result=$this->CategoryModel->validate('common/Post/Category')->isUpdate(true)->save($params);
            if($result === FALSE)
            {
                $this->error($this->CategoryModel->getError());
                exit;
            }
            $this->success();
            exit;
        }
        return $this->view->fetch();
    }

    public function del($ids = null)
    {
        $ids = !empty($ids) ? explode(',', $ids) : [];
        $row = $this->CategoryModel->column('id');
        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error('帖子分类不存在');
                exit;
            }
            $where=[
                'cateid'=>$item
            ];
            $post=$this->PostModel->where($where)->column('id');
        }
        // halt(collection($post)->toArray());
        $result = $this->CategoryModel->destroy($ids);
        if ($result === FALSE) {
            $this->error($this->CategoryModel->getError());
            exit;
        } 
        $PostStatus=$this->PostModel->destroy($post);
        if($PostStatus===FALSE)
        {
            $this->error($this->PostModel->getError());
            exit;
        }
        $this->success();
        exit;
    }
}